package com.haidechizi.i18n.code.generator.common.message.translate;

import com.haidechizi.i18n.code.generator.common.message.config.GeneratorProperties;
import com.haidechizi.i18n.code.generator.common.message.constants.TranslateTypeConstants;
import com.haidechizi.i18n.code.generator.common.message.translate.erai.ErAiUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author wangdejun
 */
@Slf4j
public class ErAiTranslate extends AbstractTranslate {

    @Autowired
    private GeneratorProperties generatorProperties;

    /**
     * 类型
     *
     * @return
     */
    @Override
    public String type() {
        return TranslateTypeConstants.ErAi;
    }

    /**
     * 翻译顶层接口
     * <p>
     * currentLanguage->language
     *
     * @param data
     * @param currentLanguage
     * @param language
     * @return
     */
    @Override
    public String translate(String data, String currentLanguage, String language) {
        return doTranslate(data, currentLanguage, language);
    }

    private String doTranslate(String data, String currentLanguage, String language) {
        String translate = ErAiUtils.translate(data, currentLanguage, language, generatorProperties.getErAiAppCode());
        if (StringUtils.isNotBlank(translate)) {
            return translate;
        }
        log.warn("翻译失败: data : {}, currentLanguage : {}, language : {}", data, currentLanguage, language);
        return null;
    }
}
